package demo.springboot.Controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import demo.springboot.POJO.T_news;
import demo.springboot.Tools.time_stamp_to_date;
import demo.springboot.service.T_newsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
@RequestMapping(value = "/news")
public class News {


    @Autowired
    T_newsService newsService;

    @GetMapping(value = "")
    public String getBookList(ModelMap map, Model model) {
        return "redirect:news/page/1";
    }

    @GetMapping(value = "/page/{pageNum}")
    public String getBookList2(ModelMap map, Model model, @PathVariable Integer pageNum ) {
        PageHelper.startPage(pageNum,3);
        List<T_news> tNews = newsService.findAll();
        for (int i = 0; i < tNews.size(); i++) {
            String dateeee = time_stamp_to_date.timeStamp_to_Date(tNews.get(i).getTime_stamp() , null);
            tNews.get(i).setTime_stamp(dateeee);
            tNews.get(i).setContent(tNews.get(i).getContent().replace("<br>", ""));
        }
        PageInfo<T_news> pageInfo = new PageInfo<>(tNews);
        model.addAttribute("pageInfo" , pageInfo);
        return "News/index.html";
    }




    @GetMapping("/id/{id}")
    public String get(@PathVariable Integer id , ModelMap map, Model model) {
        T_news neww = newsService.findById(id);
        String dateeee = time_stamp_to_date.timeStamp_to_Date(neww.getTime_stamp(), null);
        neww.setTime_stamp(dateeee);
        model.addAttribute("neww", neww);
        return "News/news_detail.html";
    }
}
